<template>
  <!-- 添加或修改岗位对话框 -->
  <el-dialog :title="title" :visible.sync="visible" width="500px" append-to-body :close-on-click-modal="false">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="项目名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入项目名称" />
      </el-form-item>

      <el-form-item label="节目平台" prop="platform">
        <el-input v-model="form.platform" placeholder="请输入节目平台" />
      </el-form-item>

      <el-form-item label="项目状态" prop="projectStatus">
        <el-radio-group v-model="form.projectStatus">
          <el-radio v-for="dict in dict.type.sys_normal_disable" :key="dict.value" :label="dict.value">{{ dict.label
          }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="维度" prop="remark">
        <el-tag :key="tag.key" v-for="tag in dynamicTags" closable :disable-transitions="false"
          @close="handleClose(tag.key)">
          {{ tag.key }}
        </el-tag>
        <el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue" ref="saveTagInput" size="small"
          @keyup.enter.native="handleInputConfirm" @blur="handleInputConfirm">
        </el-input>
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 新维度</el-button>
      </el-form-item>

      <el-form-item label="授权用户" prop="code">
        <el-select style="width: 100%" v-model="form.authorIds" multiple placeholder="请选择">
          <el-option v-for="item in userOptions" :key="item.userId" :label="item.nickName" :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addProject, updateProject, getAuthorList } from '@/api/system/project'
export default {
  dicts: ['sys_normal_disable', 'sys_project_platform'],
  props: {},
  mixins: [],
  components: {},
  data() {
    return {
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      visible: false,
      // 表单参数
      form: {
        name: '',
        platform: '',
        projectStatus: '',
        authorIds: [],
        projectScore: ''
      },
      // 表单校验
      rules: {},
      dynamicTags: [],
      inputVisible: false,
      inputValue: '',
      userOptions: [],
      platformOptions: [],
    }
  },
  computed: {
    // Your computed here
  },
  watch: {
    // Your watch here
  },
  methods: {
    open(title, formData) {
      this.getUserList()
      this.visible = true
      this.form = {
        ...formData,
        projectStatus: String(formData.projectStatus)
      }
      if (formData.projectScore) {
        this.dynamicTags = JSON.parse(formData.projectScore)
      } else {
        this.dynamicTags = []
      }

      if (this.form.authorIds) {
        this.form.authorIds = this.form.authorIds.map(r => {
          if (this.$store.state.user.id != r) {
            return r;
          }
        })
      }
      this.title = title
    },
    // 取消按钮
    cancel() {
      this.visible = false
      this.form = {
        name: '',
        platform: '',
        projectStatus: '',
        authorIds: [],
        projectScore: ''
      }
    },
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
    },

    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },

    handleInputConfirm() {
      let inputValue = this.inputValue
      if (inputValue) {
        this.dynamicTags.push({
          key: inputValue,
          value: ''
        })
      }
      this.inputVisible = false
      this.inputValue = ''
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.projectScore = JSON.stringify(this.dynamicTags)
          console.log(this.form)
          if (this.form.id != undefined) {
            updateProject(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.$emit('update')
              this.cancel()
            })
          } else {
            addProject(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.$emit('update')
              this.cancel()
            })
          }
        }
      })
    },
    getUserList() {
      getAuthorList().then(response => {
        this.userOptions = response.rows;
      })
    }
  },
  created() {
    // console.log('Component created');
  },
  mounted() {
    // console.log('Component created');
  }
}
</script>

<style scoped>
/* Your styles here */
.el-tag+.el-tag {
  margin-left: 10px;
}

.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}

.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
